<template>
    <div class="marklist">
        <el-scrollbar max-height="100%">
            <div class="marker" v-for="(marker, index) in currectPage?.markerList" :key="index" :style="{
                backgroundColor: marker.selected ? '#666' : '#333',
                padding: marker.pid == '' ? '5px' : '10px',
            }">
                <el-image :src="currectPage.pageImg +
                    '?x-oss-process=image/crop,x_' +
                    Math.round(marker.shape.x) +
                    ',y_' +
                    Math.round(marker.shape.y) +
                    ',w_' +
                    Math.round(marker.shape.width) +
                    ',h_' +
                    Math.round(marker.shape.height) +
                    ',x-oss-process=image/resize,w_200,m_lfit'
                    " fit="contain" @click="selectFeature(index)" />
                <el-image v-for="(cmarker, index) in marker.childs" :key="index" :src="getPageImgUrlByMark(cmarker) +
                    '?x-oss-process=image/crop,x_' +
                    Math.round(cmarker.shape.x) +
                    ',y_' +
                    Math.round(cmarker.shape.y) +
                    ',w_' +
                    Math.round(cmarker.shape.width) +
                    ',h_' +
                    Math.round(cmarker.shape.height) +
                    ',x-oss-process=image/resize,w_200,m_lfit'
                    " fit="contain" />
                <div class="operate w-full mt-2 flex flex-row justify-between">
                    <el-select v-model="marker.markType" size="small" style="width: 60px"
                        @change="onMarkTypeChanged(marker)">
                        <el-option v-for="(item,index) in options" :key="index" :label="item.label" :value="item.value" />
                    </el-select>
                    <el-button size="small" type="info" @click.stop="handleEditContext(marker);" v-if="marker.pid == ''">内容
                    </el-button>
                    <el-button size="small" type="info" @click.stop="handleDelete(marker)" :icon="Delete"></el-button>
                </div>
            </div>
        </el-scrollbar>
    </div>

    <el-dialog width="60%" v-model="dialogLinkSourceVisible" draggable :title="'【资源挂接】'">
        <LinkSourceVue :link-type="linkSourceData.linkType" :link-id="linkSourceData.linkId"></LinkSourceVue>
    </el-dialog>
</template>

<script lang="ts" setup>
import LinkSourceVue from '@/views/wisdbook/marker/linksource/index.vue'
import { ref, reactive, defineProps, defineEmits } from "vue";
import { Delete } from "@element-plus/icons-vue";
import { storeToRefs } from "pinia";
import { useBookEditorStore } from "@/stores/modules/wisdbookMarker";
let { pages, currectPage, markTypes } = storeToRefs(useBookEditorStore());
// const props = defineProps({
// });
const emit = defineEmits(['onSelectFeature','onMarkTypeChanged', 'onHandleDelete','handleEditContext'])
const options = [
    {
        value: "0",
        label: "标题",
    },
    {
        value: "1",
        label: "正文",
    },
    {
        value: "2",
        label: "题目",
    }
]

const dialogLinkSourceVisible = ref(false)

const linkSourceData=reactive({
    title:'',
    linkType:'0',
    linkId:'0'
})

const selectFeature = (marker: any) => {
  emit('onSelectFeature', marker);
}

const onMarkTypeChanged = (marker: any) => {
    emit('onMarkTypeChanged', marker);
}

const handleEditContext = (marker: any) => {
    // emit('handleEditContext', marker);
    linkSourceData.linkId=marker.id+''
    linkSourceData.title=''
    dialogLinkSourceVisible.value=true
}

const handleDelete = (marker: any) => {
    emit('onHandleDelete', marker);
}

const getPageImgUrlByMark = (marker: any) => {
    const page = pages.value.find((ele) => ele.id === marker.pageId);
    return page?.pageImg;
};
</script>
<style scoped>
.marklist .marker {
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    background-color: #333;
    border-bottom: 2px solid #000;
}

.marklist .marker:hover {
    cursor: pointer;
    background-color: #666;
}

.marklist .marker .el-image {
    display: block;
    height: auto;
}
</style>
